package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.UserInfo;
import com.ipfsemc.ipfsemc.util.RespList;
import com.ipfsemc.ipfsemc.util.Response;
import com.ipfsemc.ipfsemc.wx.SignUtil;
import com.ipfsemc.ipfsemc.wx.WxLogin;
import com.ipfsemc.ipfsemc.wx.menu.MenuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.weixin4j.Configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/login")
@Api(value = "WxLoginController", tags = "微信登陆，菜单创建")

@Slf4j
public class WxLoginController {

    @Autowired
    private SignUtil signUtil;

    @Autowired
    private WxLogin wxLogin;


    @Autowired
    private MenuUtil menuUtil;


    @ApiOperation(value = "创建微信菜单",httpMethod = "GET")

    @RequestMapping("/createMenu")
    @ResponseBody
    public Map<String, Object> createMenu(HttpServletRequest request) {


        log.info("WeixinController.createMenu");
        String ctx = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        log.info("ctx=" + ctx);

        return menuUtil.initMenu(ctx);
    }


//    @RequestMapping("/test1")
//    @ResponseBody
//    public Map<String, Object> test1() {
//        log.info("WeixinController.test1");
//        Map<String, Object> jsonData = new HashMap<>();
//        System.out.println("test");
//        String s1 = Configuration.getProperty("weixin4j.message.handler.normal");
//        String s2 = Configuration.getProperty("weixin4j.message.handler.event");
//
//        jsonData.put("weixin4j.message.handler.normal", s1);
//        jsonData.put("weixin4j.message.handler.event", s2);
//        return jsonData;
//    }


    @ApiOperation(value = "微信接入",httpMethod = "GET")
    @GetMapping("/weixinToken")
    public void get(HttpServletRequest request, HttpServletResponse response) {
        // 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");

        PrintWriter out = null;
        try {
            out = response.getWriter();
            // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，否则接入失败
            if (signUtil.checkSignature(signature, timestamp, nonce)) {
                out.print(echostr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    @ApiOperation(value = "微信扫码登陆",httpMethod = "GET")

    @GetMapping("/wxLogin")
    public void wxLogin(HttpServletRequest request, HttpServletResponse response) {
        wxLogin.WxLogin(request, response);
    }

    @ApiOperation(value = "回调",httpMethod = "GET")

    @GetMapping("/loginBack")
    public Response<UserInfo> LoginBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Response re=new Response();
        UserInfo userInfo = wxLogin.LoginBack(request, response);
        re.setResult(userInfo);
        return re;
    }



}